//
//  PictureView.swift
//  SinaBlog
//
//  Created by macs on 15/11/14.
//  Copyright © 2015年 BlackTail. All rights reserved.
///Users/mac/Desktop/iPhone 项目/XFXLBlog/SinaBlog/SinaBlog/Classses/Module/Home/StatusCell/PictureView.swift:36:1: Type 'PictureView' does not conform to protocol 'UICollectionViewDataSource'

import UIKit
import SnapKit
import SDWebImage

let picCellId = "picCellId"

let picCellMargin : CGFloat = 5
class PictureView: UICollectionView {
    
    var imageUrls : [NSURL]? {
        
        didSet {
            
            reloadData()
            
            let viewSize = updatePictureViewSize()
            self.snp_updateConstraints { (make) -> Void in
                make.width.equalTo(viewSize.width)
                make.height.equalTo(viewSize.height)
            }
            //testLable.text = "\(imageUrls?.count ?? 0)"
        }
    }
    
    //计算视图大小
    func updatePictureViewSize() -> CGSize{
        
        //列数
        let colm : CGFloat  = 3.0000001
        let maxWidth = screenW - 2 * statuscellMargin
        //计算多张图片宽度是单张最大宽度
        let picWidth = (maxWidth - 2 * picCellMargin) / colm
        //        print(picWidth)
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSizeMake(picWidth, picWidth) //CGSize(width: picWidth, height: picWidth)
        
        
        
        let imageCount = imageUrls?.count ?? 0
        if imageCount == 0 {
            return CGSizeZero
        }
        //单张图片
        if imageCount == 1 {
            //            let viewSize = CGSize(width: 150, height: 180)
            //获取单张图片
            let key = imageUrls?.last?.absoluteString
            let image = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(key!)
            
           //print(image)
           // var imageSize = CGSize(width: 150, height: 180)
           let  imageSize = image.size
            layout.itemSize = imageSize
            return imageSize
        }
        //4张图片
        if imageCount == 4 {
            let w = picWidth * 2 + picCellMargin
            return CGSize(width: w, height: w)
        }
        
        //多张.多行图片时
        //计算行数
        let row = CGFloat((imageCount - 1) / 3 )
        let ws = maxWidth
        let hs = (row + 1) * picWidth + row  * picCellMargin
        
        return CGSize(width: ws, height: hs)
    }
    
    //3.重写构造方法
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = picCellMargin
        layout.minimumInteritemSpacing = picCellMargin
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        setupUI()
        self.dataSource = self
        registerClass(PictureCell.self, forCellWithReuseIdentifier: picCellId)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //2.添加子控件,设置约束
    func setupUI() {
        
        //  backgroundColor = UIColor.randomColor()
        backgroundColor = UIColor.whiteColor()
        //UIColor.redColor()
        addSubview(testLable)
        
        testLable.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
    }
    
    //1.懒加载控件
    lazy var testLable : UILabel = {
        
        let l = UILabel()
        l.font = UIFont.systemFontOfSize(20)
        l.textColor = UIColor.redColor()
        
        return l
    }()
    
    
}

extension PictureView : UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(picCellId, forIndexPath: indexPath) as! PictureCell
        
        cell.url = imageUrls![indexPath.item]
        
        return cell
    }
    
}

class PictureCell : UICollectionViewCell {
    
    var url : NSURL? {
        
        didSet {
            
            iconView.sd_setImageWithURL(url)
        }
        
    }
    
    //3.重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //2.添加子控件,设置约束
    func setupUI () {
        
        contentView.addSubview(iconView)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    //1.懒加载控件
    lazy var iconView : UIImageView = {
        
        let image = UIImageView()
        //设置图片显示样式
        image.contentMode = .ScaleAspectFill
        image.clipsToBounds = true
        return image
    }()
}

